package cz.anime.ppro.beans;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.faces.bean.ManagedBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import cz.anime.ppro.enums.UserRolesEnum;
import cz.anime.ppro.logic.UserComponent;
import cz.anime.ppro.model.Fragment;
import cz.anime.ppro.model.Webuser;

@Component
@ManagedBean
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class CreateUserBean {

	@Autowired
	private UserComponent userComponent;

	private List<Webuser> users;
	
	private String name;
	private String surname;
	private String nick;
	private String password;
	private String passwordAgain;
	private String city;
	private String mail;
	
	private boolean error = false;
	private String errorMessage;

	public String createUser(){
		users = userComponent.getUsers();
		Webuser user = new Webuser();
		
		for (Webuser webuser : users) {
			if (webuser.getNick().equals(nick)) {
				error = true;
				errorMessage = "Nick už existuje!";
				return "create-user";
			}
			if (webuser.getMail() != null && webuser.getMail().equals(mail)) {
				error = true;
				errorMessage = "Mail už někdo využívá!";
				return "create-user";
			}
			user.setNick(nick);
			user.setMail(mail);
		}
		
		if (password != null && !password.equals(passwordAgain)) {
			error = true;
			errorMessage = "Hesla se neshodují!";
			return "create-user";
		}
		
		user.setPassword(password);
		user.setName(name);
		user.setSurname(surname);
		user.setCity(city);
		user.setDate(Calendar.getInstance().getTime());
		user.setFragments(new ArrayList<Fragment>());
		user.setRole(UserRolesEnum.ROLE_USER.name());
		
		userComponent.createUser(user);
		
		return "home?faces-redirect=true";
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getNick() {
		return nick;
	}

	public void setNick(String nick) {
		this.nick = nick;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPasswordAgain() {
		return passwordAgain;
	}

	public void setPasswordAgain(String passwordAgain) {
		this.passwordAgain = passwordAgain;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public boolean isError() {
		return error;
	}

	public void setError(boolean error) {
		this.error = error;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	
}
